<!DOCTYPE html>

<!--
   Copyright 2014 Google Inc. All rights reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<html>
<head>
<meta charset="UTF-8">
<title>IO Schedule updater</title>
<link rel="stylesheet" type="text/css" href="../index.css"></link>
</head>
<body>
  <div class="actions">
    <a id="productionManifest" href="#" class="linkasbutton see_manifest">see production manifest</a>
    <a id="stagingManifest" href="#" class="linkasbutton see_manifest">see staging manifest</a>
    <a href="runupdate" target="_blank" class="run linkasbutton">Update now</a>
    <a href="runupdate?force=true" target="_blank" class="runForced error linkasbutton">Force
      Update</a>
  </div>
  <table class="log_table">
    <tr class="table_header logline">
       <th class="date">Date</td>
       <th class="forced">Forced</td>
       <th class="filename">Filename</td>
       <th class="version">Version</td>
       <th class="summary">Summary</td>
       <th class="timings">Time taken</td>
    </tr>
    <tr id="placeholder" class="logline">
       <td class="date"></td>
       <td class="forced"></td>
       <td><a href="#" class="filename"></a><br>
         <small>hash <span class="hash"></span></small>
       </td>
       <td class="version"></td>
       <td class="summary"></td>
       <td class="time"><span class="totaltime"></span> ms</td>
    </tr>
  </table>
  <div class="log">
    <textarea></textarea>
  </div>
</body>

<script>
  document.addEventListener('DOMContentLoaded', function() {
    document.body.classList.add('waiting');
    var request = new XMLHttpRequest();
    request.open('GET', '/admin/schedule/log_data');
    request.responseType = 'json';
    request.onload = function(e) { processLogData(e.target.response); };
    request.send();
    });

   function processLogData(logData) {
     document.body.classList.remove('waiting');
     var seeManifest = document.querySelector('#productionManifest');
     seeManifest.href="http://storage.googleapis.com/"+logData.productionManifest;
     seeManifest.addEventListener('click', seeFile);
     
     seeManifest = document.querySelector('#stagingManifest');
     seeManifest.href="http://storage.googleapis.com/"+logData.stagingManifest;
     seeManifest.addEventListener('click', seeFile);

     var placeholder = document.querySelector('#placeholder');
     var hasData = logData.lastruns.length > 0;
     document.querySelector('.log_table').style.display = hasData?'block':'none';

     logData.lastruns.forEach(function(el) {
       var line = placeholder.cloneNode(true);
       line.removeAttribute("id");
       document.querySelector('.log_table tbody').appendChild(line);
       line.querySelector('.date').innerText = new Date(el.date+" GMT");
       line.querySelector('.forced').innerText = el.forced;
       var filename = line.querySelector('.filename');
       filename.innerText = el.filename;
       filename.href = "http://storage.googleapis.com/"+logData.bucket+"/"+el.filename;
       filename.addEventListener('click', seeFile);
       line.querySelector('.hash').innerText = el.hash;
       line.querySelector('.version').innerText = el.majorVersion + '.' + el.minorVersion;
       line.querySelector('.summary').innerText = el.summary;
       var timings = '';
       var totalTime = 0;
       for (var key in el.timings) {
         timings += key+"="+el.timings[key]+"ms\n";
         totalTime += el.timings[key];
       }
       line.querySelector('.totaltime').innerText = totalTime;
       line.querySelector('.time').title = timings;

    });
   }

   function seeFile(e) {
     var xhr = new XMLHttpRequest();
     xhr.open('GET', e.target.href);
     xhr.responseType = 'json';
     xhr.onload = function(ev) {
       var area = document.querySelector('.log textarea');
       area.innerText = JSON.stringify(ev.target.response, null, 2);
     };
     xhr.onerror = function(ev) {
       var area = document.querySelector('.log textarea');
       area.innerText = "Could not load "+e.target.href;
     };
     xhr.send();
     e.preventDefault();
   }
</script>
</html>
